<?php
require_once('../../config.php');
require_js(array('yui_yahoo', 'yui_dom', 'yui_event', 'yui_connection', 'yui_json'));
require_once("$CFG->dirroot/blocks/moodlelibreserve/lib.php");
$rec_per_pg = 8;
$offset = optional_param('offset', 0, PARAM_INT);
//We need the course id so we can return to it.
$cid = required_param('cid', PARAM_INT);
$from_course = get_record('course', 'id', $cid);
$curr_page = ceil(($offset/$rec_per_pg)+1);
$res_count = get_record_sql("SELECT count(*) AS count FROM {$CFG->prefix}libres WHERE user_id={$USER->id}");

global $CFG, $USER;
echo '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/blocks/moodlelibreserve/style.css" />';
$context = get_context_instance(CONTEXT_COURSE, $cid);

require_js($CFG->wwwroot.'/blocks/moodlelibreserve/libres.js');

$cat_id = array('', 'Call No.', 'ISBN No.', 'Instructor\'s copy',     'Call No.', 'ISBN No.', 'Instructor\'s copy', 'Call No.', 'ISBN No    .', 'Instructor\'s copy');
$cat_tb = array('', 'call_num', 'isbn_num', 'instr_copy', 'call_num', 'isbn_num', 'instr_copy', 'call_num', 'isbn_num', 'instr_copy');
$months = array('', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

$nav[] = array('name' => $from_course->fullname, 'link' => '../../course/view.php?id='.$from_course->id);
$nav[] = array('name' => 'Library Reserve', 'link' => '');
$navigation = build_navigation($nav);
print_header('Physical reserve requests', 'Physical reserve requests', $navigation);


if(has_capability('block/moodlelibreserve:makerequest', $context)){
	$rs = get_recordset('request_types');
	$course = get_record('course', 'id', $cid);
	echo '<a name="request_box">M</a>ake a new request for '.$course->fullname.':';
	$radio_text = '<div id="libres-form" style="border:1px solid #ccc;padding:0.5em;font-size:0.8em;">initializing...';
	$radio_text .= '<script type="text/javascript" src="'.$CFG->wwwroot.'/blocks/moodlelibreserve/libres.js"></script>';
	$radio_text .= '<script type="text/javascript">init_libres_form(1, "'.$CFG->wwwroot.'", '.$cid.');</script></div>';
	echo $radio_text;

	$rs = get_records('libres', 'user_id', $USER->id, 'timecreated', '*', $offset, $rec_per_pg);
	echo "Pending requests:";
	echo libres_pagination($res_count->count, $rec_per_pg, 'instructor.php', 'cid='.$cid, $curr_page);
	echo '<div id="libres_container" style="padding:5px;">';
	foreach($rs as $op){
		echo '<div id="reserve_box_'.$op->id.'" class="reserve_box">';
		$course = get_record_sql("SELECT fullname FROM {$CFG->prefix}course WHERE id=$op->course");
		$timecreated = date("Y-m-d H:i:s", $op->timecreated);
		$date = new DateTime($timecreated);
		$date->modify('+14 days');
		$month = $date->format("m");
		echo "<p style=\"color:red;font-weight:bold;font-size:14px;\">Will be processed by: ".$months[intval($month)]." ".$date->format("d Y")."</p>";
		echo "<p><b>Course:</b> $course->fullname</p>";
		$user = get_record_sql("SELECT firstname, lastname, email FROM {$CFG->prefix}user WHERE id=$op->user_id");
		$courses = get_my_courses($USER->id);
		echo '<p>Set to another course:</p>';
		echo '<select id="course_select">';
		foreach($courses as $course){
			echo '<option value="'.$course->id.'">'.$course->fullname.'</option>';
		}
		echo '</select>';

		echo "<p><b>Instructor:</b> $user->firstname $user->lastname (<a href=\"mailto:$user->email\">$user->email</a>)</p>";
		$req_type = get_record_sql("SELECT description FROM {$CFG->prefix}request_types WHERE id=$op->req_type");
		echo "<p><i>$req_type->description</i></p>";
		$req_type = $op->req_type;
		echo "<div style=\"background-color:#ccc;padding:2px;\"><b>$op->title, $op->author</b></div>";
		echo "<p><b>$cat_id[$req_type]:</b> ".$op->{$cat_tb[$req_type]}."</p>";
		if($op->processing == 0){
			echo '<p><a href="#request_box" onmousedown="get_reserve('.$op->id.', \''.$CFG->wwwroot.'\');">Edit</a>&nbsp;&nbsp;&nbsp;<a onmousedown="delete_request('.$op->id.', \''.$CFG->wwwroot.'\');return false;">Delete</a></p>';
		}
		echo libres_comments('', $op->id, 'form');
		echo '</div>';
	}
	echo '<br style="height:0px;clear:both;" />';
	echo '</div>';
}else{
	echo '<div style="font-size:14px;font-weight:bold;color:red;">Sorry, you do not have permission to view this page.</div>';
}
print_footer();
?>
